import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-18
 * Time: 20:43
 */
public class MinStack {
    private Stack<Integer> minStack;
    private Stack<Integer> stack;
    public MinStack() {
        minStack = new Stack<>();
        stack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minStack.empty()) {
            minStack.push(val);
        } else {
            if(val <= minStack.peek()) {
                minStack.push(val);
            }
        }
    }

    public void pop() {
        int temp = stack.pop();
        if(temp == minStack.peek()) {
            minStack.pop();
        }
    }

    public int top() {
        if(stack.empty()) {
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        if(minStack.empty()) {
            return -1;
        }
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack1 = new MinStack();
        minStack1.push(-2);
        minStack1.push(0);
        minStack1.push(1);
        int min = minStack1.getMin();
        minStack1.pop();
        int top = minStack1.top();
        System.out.println("min = " + min + " top = " + top);
    }
}
